Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
Cannot read properties of undefined (reading > 'map')

I'm fetching data from an API and want to show them as texts, the title and desc worked fine but the categories didn't, the error I get is:

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

The Code:

export default function SinglePost() {

      const [post, setPost] = useState({});
      const [title, setTitle] = useState("");
      const [desc, setDesc] = useState("");
      const [categories, setCategories] = useState("");
      
        useEffect(() => {
        const getPost = async () => {
          const res = await axios.get("/posts/" + path);
          setPost(res.data);
          setTitle(res.data.title);
          setDesc(res.data.desc);
          setCategories(res.data.categories);
        };
        getPost()
      }, [path]);
      
      <div className="singlePost">
                <div className="singlePostInfo">
               <div className="postCats">
              {post.categories.map((c) => (
                    <span className="postCat" key={c._id}>
                      Category: {c.name}
                      </span>
                  ))}
              </div> 
      </div>
    }

I don't know where my mistake is!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you need to update your render method to render categories from the state and the default value of categories state should be [] instead of ""

Change const [categories, setCategories] = useState("") to const [categories, setCategories] = useState([])

Change post.categories.map to categories.map and it should work!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda